﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NPMonitor.Interfaces;
using NPMonitor.DataModel.Users;
using NPMonitor.Implementation.Internal;

namespace NPMonitor.Implementation.Store
{
    public class UserStore : IUserStore
    {
        public List<Users> GetAllUsers(int pageIndex, int pageSize)
        {
            if (pageIndex < 0)
            {
                pageIndex = 0;
            }

            if (pageSize < 0)
            {
                pageSize = int.MaxValue;
            }
            using (var ctx = new DbStoreContext())
            {
                var users = from p in ctx.User
                            select p;
                return users.OrderByDescending(d => d.UserId).Skip(pageIndex * pageSize).Take(pageSize).ToList();
            }
        }

        public int GetUsersCounts()
        {
            using (var ctx = new DbStoreContext())
            {
                return ctx.User.Count();
            }
        }

        public void DeleteUser(int userId)
        {
            using (var ctx = new DbStoreContext())
            {
                var user = ctx.User.FirstOrDefault(d => d.UserId == userId);
                ctx.User.Remove(user);
                ctx.SaveChanges();
            }
        }

        public void ChangeUserStatus(int userId, UserStatus userStatus)
        {
            using (var ctx = new DbStoreContext())
            {
                var user = ctx.User.FirstOrDefault(d => d.UserId == userId);
                user.Status = userStatus.ToString();
                ctx.SaveChanges();
            }
        }

        public Users GetUserInfoById(int userId)
        {
            using (var ctx = new DbStoreContext())
            {
                var user = ctx.User.FirstOrDefault(d => d.UserId == userId);
                return user;
            }
        }

        public Users GetUserInfoByUsername(string username)
        {
            using (var ctx = new DbStoreContext())
            {
                var user = ctx.User.FirstOrDefault(d => d.UserName == username);
                return user;
            }
        }

        public Users GetUserInfoByAccount(string userAccount)
        {
            using (var ctx = new DbStoreContext())
            {
                var user = ctx.User.FirstOrDefault(d => d.Account == userAccount);
                return user;
            }
        }

        public void SaveUser(int id, Users user)
        {
            using (var ctx = new DbStoreContext())
            {
                if (id == 0)
                {
                    ctx.User.Add(user);
                }
                else
                {
                    var specificUser = ctx.User.FirstOrDefault(d => d.UserId == id);
                    if (specificUser != null)
                    {
                        specificUser.UserName = user.UserName;
                        specificUser.Mail = user.Mail;
                        specificUser.Status = user.Status;
                        specificUser.Account = user.Account;
                    }
                }
                ctx.SaveChanges();
            }
        }

        public bool CheckAccountName(string accountName, int userId)
        {
            using (var ctx = new DbStoreContext())
            {
                var user = ctx.User.FirstOrDefault(d => d.Account == accountName && d.UserId != userId);
                if (user != null)
                {
                    return true;
                }
                else
                    return false;
            }
        }

        public bool CheckUserName(string userName, int userId)
        {
            using (var ctx = new DbStoreContext())
            {
                var user = ctx.User.FirstOrDefault(d => d.UserName == userName && d.UserId != userId);
                if (user != null)
                {
                    return true;
                }
                else
                    return false;
            }
        }
    }
}
